<template>
	<view class="course">
		<!-- 头部区域 -->
		<view class="header_box">
			<view class="search">
				<u-search placeholder="搜索课程" v-model="query.search" @change="changeSearch" :show-action="false" shape="square"></u-search>
			</view>
			<view class="tabs" :class="{ loading: !tabDataLoaded }">
				<view v-for="(item,index) in tabsList" :key="index" 
					class="tabItem" 
					@click="changeTabs(index)" 
					:class="{ active: tabsCurrent === index }"
					:style="{borderBottom: tabsCurrent === index ? '#73F0EA 8rpx solid' : ''}">
					<view class="size"
						:style="{
							color: tabsCurrent === index ? '#73F0EA' : '#999999',
							fontWeight: tabsCurrent === index ? '800' : '400'
						}">
						{{item.name}} 
					</view>
				</view>
			</view>
		</view>
		
		<!-- 内容区域 -->
		<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="scrolltolower">
			<!-- 加载状态 -->
			<view v-if="loading" class="loading-container">
				<view class="loading-card" v-for="n in 3" :key="n" :style="{ animationDelay: (n-1) * 0.2 + 's' }">
					<view class="skeleton-course"></view>
				</view>
			</view>
			
			<!-- 错误状态 -->
			<view v-else-if="loadError" class="error-container">
				<view class="error-content">
					<u-icon name="wifi-off" size="60" color="#ccc"></u-icon>
					<text class="error-text">加载失败，请重试</text>
					<u-button type="primary" size="medium" @click="retryLoad">重新加载</u-button>
				</view>
			</view>
			
			<!-- 课程列表 -->
			<view v-else-if="courseList.length" class="comlum_box">
				<view v-for="(item,index) in courseList" :key="index" 
					class="Item"
					:style="{ animationDelay: index * 0.1 + 's' }">
					<view class="header">
						<image :src="item.category_image" mode="aspectFill" v-if="item.category_image" class="tag"></image>
						<view class="title">
							{{item.shop_name}}
						</view>
						<view class="status" :style="{color:tabsCurrent == 1 ?'#999999':'#73F0EA'}">
							{{item.form == 'unfinished' ?'进行中':'已完成'}}
						</view>
					</view>
					<view class="bottom">
						<view class="class">
							<image :src="item.image" mode="aspectFill" class="img"></image>
							<view class="right-Item">
								<view class="title">
									{{item.title}}
								</view>
								<view class="size">
									<text class="color1">第</text>
									<text class="color2">{{item.ynmt}}</text>
									<text class="color1">丨共{{item.classes}}节</text>
								</view>
								<view class="size">
									<text class="color1">{{item.time_str}}</text>
								</view>
							</view>
						</view>
						<view class="foot">
							<view class="left">
								<image :src="item.coach_avatar" mode="" class="avater"></image>
								<view class="leftName">
									{{item.coach_nickname}}
								</view>
							</view>
							<view class="right">
								<view class="Class-record" @click="$ROUTELINK('Class_record?my_course_id=' + item.my_course_id)">
									上课记录
								</view>
								<view class="Class-begins" v-if="!TabsCurrent" @click="openQrCode(item)">
									开始上课
								</view>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" />
			</view>
			<empty v-else :content="!TabsCurrent ?'进行中课程':'已完成课程'"></empty>
		</scroll-view>
		<popup :popupShow="popupShow" @close="close" :code="code"></popup>
	</view>
</template>

<script>
	import popup from './components/popup.vue'
	import empty from './components/empty.vue'
	import {
		mycourseInfo,
		mycourseQode
	} from "@/api/user.js"
	export default {
		components: {
			popup,
			empty
		},
		data() {
			return {
				// 查询参数
				query: {
					form: 'unfinished',
					search: '',
					page: 1
				},
				// 弹窗控制
				popupShow: false,
				// 标签页配置
				tabsList: [
					{ name: '进行中', form: 'unfinished' },
					{ name: '已完成', form: 'finish' }
				],
				// 二维码数据
				code: '',
				// 当前选中的标签页索引
				tabsCurrent: 0,
				// 课程列表数据
				courseList: [],
				// 加载更多状态
				status: 'loadmore',
				// 页面状态管理
				loading: false,
				loadError: false,
				// 标签页数据加载状态
				tabDataLoaded: false,
				// 搜索防抖定时器
				searchTimer: null
			}
		},
		computed: {
			/**
			 * 获取空状态显示文本
			 */
			getEmptyText() {
				const emptyTexts = [
					'暂无进行中课程',
					'暂无已完成课程'
				]
				return () => emptyTexts[this.tabsCurrent] || emptyTexts[0]
			}
		},
		onLoad() {
			this.initializePageData()
		},
		methods: {
			/**
			 * 初始化页面数据
			 */
			async initializePageData() {
				try {
					this.loading = true
					this.loadError = false
				
					// 并行加载标签页数据和课程列表
					await Promise.all([
						this.getTabData(),
						this.getMyCourseInfo()
					])
				} catch (error) {
					console.error('页面初始化失败:', error)
					this.loadError = true
					uni.showToast({
						title: '加载失败，请重试',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
		
			/**
			 * 搜索变化处理（防抖）
			 */
			changeSearch() {
				if (this.searchTimer) {
					clearTimeout(this.searchTimer)
				}
			
				this.searchTimer = setTimeout(() => {
					this.courseList = []
					this.query.page = 1
					this.getMyCourseInfo()
				}, 500) // 500ms防抖
			},
		
			/**
			 * 切换标签页
			 * @param {number} index - 标签页索引
			 */
			changeTabs(index) {
				if (this.tabsCurrent === index || this.loading) return
			
				this.tabsCurrent = index
				this.courseList = []
				this.query.page = 1
				this.query.form = this.tabsList[index].form
				this.getMyCourseInfo()
			},
		
			/**
			 * 关闭弹窗
			 */
			close() {
				this.popupShow = false
			},
		
			/**
			 * 打开二维码弹窗
			 * @param {Object} item - 课程数据
			 */
			async openQrCode(item) {
				if (!item || !item.my_course_id) {
					uni.showToast({
						title: '课程信息异常',
						icon: 'none'
					})
					return
				}
			
				try {
					const { data: res } = await mycourseQode({ my_course_id: item.my_course_id })
					if (res.code === 1) {
						this.code = res.data.qrcode
						this.popupShow = true
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg || '获取二维码失败'
						})
					}
				} catch (error) {
					console.error('获取二维码失败:', error)
					uni.showToast({
						icon: 'none',
						title: '网络异常，请重试'
					})
				}
			},
				
			/**
			 * 获取标签页数据（包含数量统计）
			 */
			async getTabData() {
				try {
					const { data: res } = await mycourseInfo(this.query)
					if (res.code === 1) {
						this.tabsList[0].name = `进行中(${res.data.unfinished_num || 0})`
						this.tabsList[1].name = `已完成(${res.data.finish_num || 0})`
						this.tabDataLoaded = true
					} else {
						throw new Error(res.msg || '获取标签页数据失败')
					}
				} catch (error) {
					console.error('获取标签页数据失败:', error)
					uni.showToast({
						title: '获取标签数据失败',
						icon: 'none'
					})
					throw error
				}
			},
				
			/**
			 * 滚动到底部加载更多
			 */
			scrolltolower() {
				if (this.status === 'nomore' || this.loading) return
					
				this.query.page++
				this.getMyCourseInfo()
			},
				
			/**
			 * 获取我的课程信息
			 */
			async getMyCourseInfo() {
				try {
					const { data: res } = await mycourseInfo(this.query)
					if (res.code === 1) {
						this.status = 'loading'
							
						if (this.query.page > res.data.all_page) {
							this.status = 'nomore'
							return
						}
							
						this.status = 'more'
						this.courseList = this.courseList.concat(res.data.list || [])
					} else {
						throw new Error(res.msg || '获取课程列表失败')
					}
				} catch (error) {
					console.error('获取课程列表失败:', error)
					uni.showToast({
						title: '获取课程列表失败',
						icon: 'none'
					})
					throw error
				}
			},
				
			/**
			 * 重新加载页面数据
			 */
			retryLoad() {
				this.initializePageData()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #e9e9e9;
		// 全局关闭点击高亮效果
		-webkit-tap-highlight-color: transparent;
		tap-highlight-color: transparent;
	}
	
	// 全局关闭所有可点击元素的高亮效果
	* {
		-webkit-tap-highlight-color: transparent;
		tap-highlight-color: transparent;
		outline: none;
		-webkit-touch-callout: none;
	}

	.header_box {
		width: 750rpx;
		background: #ffffff;
		box-shadow: 0rpx 8rpx 28rpx 0rpx rgba(199, 205, 179, 0.15);
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 24rpx;

		.search {
			width: 100%;
			padding: 24rpx;
			margin-bottom: 6rpx;
		}

		.tabs {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-around;
			transition: all 0.3s ease;
			
			&.loading {
				opacity: 0.7;
			}

			.tabItem {
				display: flex;
				flex-direction: column;
				align-items: center;
				height: 60rpx;
				position: relative;
				transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
				cursor: pointer;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				&.active {
					transform: translateY(-2rpx);
				}
				
				&:hover {
					transform: translateY(-1rpx);
				}
				
				&:active {
					background-color: transparent !important;
					box-shadow: none !important;
				}
				
				.size {
					font-size: 28rpx;
					letter-spacing: 0.62rpx;
					font-family: PingFang SC, PingFang SC-Heavy;
					transition: all 0.3s ease;
				}
			}
		}
	}

	.scroll-Y {
		width: 100%;
			height: 83vh;
		
		// 加载状态
		.loading-container {
			padding: 40rpx 24rpx;
			display: flex;
			flex-direction: column;
			gap: 20rpx;
			
			.loading-card {
				width: 100%;
				height: 280rpx;
				background: #ffffff;
				border-radius: 16rpx;
				opacity: 0;
				animation: pulse 1.5s ease-in-out infinite;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				.skeleton-course {
					width: 100%;
					height: 100%;
					background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
					background-size: 200% 100%;
					border-radius: 16rpx;
					animation: skeleton-loading 2s infinite;
				}
			}
		}
		
		// 错误状态
		.error-container {
			padding: 100rpx 24rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			// 关闭点击高亮效果
			-webkit-tap-highlight-color: transparent;
			tap-highlight-color: transparent;
			outline: none;
			-webkit-touch-callout: none;
			-webkit-user-select: none;
			user-select: none;
			
			.error-content {
				display: flex;
				flex-direction: column;
				align-items: center;
				gap: 20rpx;
				
				.error-text {
					font-size: 28rpx;
					color: #999;
					margin: 20rpx 0;
				}
			}
		}

		.comlum_box {
			width: 100%;
			display: flex;
			flex-direction: column;
			padding: 0 24rpx;

			.Item {
				width: 702rpx;
				background: #ffffff;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 0 24rpx 24rpx;
				// 动画效果
				opacity: 0;
				transform: translateY(30rpx);
				animation: fadeInUp 0.6s ease forwards;
				transition: transform 0.3s ease;
				// 关闭点击高亮效果
				-webkit-tap-highlight-color: transparent;
				tap-highlight-color: transparent;
				outline: none;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				user-select: none;
				
				&:hover {
					transform: translateY(-5rpx);
				}
				
				&:active {
					transform: scale(0.98);
					background-color: transparent !important;
					box-shadow: none !important;
				}

				.header {
					width: 100%;
					height: 86rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					border-bottom: 1rpx solid #e9e9e9;

					.tag {
						width: 114rpx;
						height: 36rpx;
						margin-right: 12rpx;
					}

					.title {
						flex: 1;
						font-size: 28rpx;
						font-family: PingFang SC, PingFang SC-Bold;
						font-weight: 700;
						text-align: left;
						color: #333333;
					}

					.status {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC-Medium;
						font-weight: 500;
					}
				}

				.bottom {
					width: 100%;
					display: flex;
					flex-direction: column;

					.class {
						width: 100%;
						display: flex;
						flex-direction: row;
						padding-top: 26rpx;

						.img {
							width: 160rpx;
							height: 120rpx;
							background: rgba(0, 0, 0, 0.00);
							border-radius: 16rpx;
							margin-right: 20rpx;
						}

						.right-Item {
							display: flex;
							flex-direction: column;
							flex: 1;

							.title {
								font-size: 32rpx;
								font-family: PingFang SC, PingFang SC-Bold;
								font-weight: 700;
								text-align: left;
								color: #333333;
								margin-bottom: 8rpx;
							}

							.size {
								font-size: 24rpx;
								font-family: PingFang SC, PingFang SC-Medium;
								font-weight: 500;
								text-align: left;
								.color1{
									color: #999999;
								}
								.color2{
									color: #73F0EA;
								}
							}
						}
					}

					.foot {
						width: 100%;
						display: flex;
						flex-direction: row;
						align-items: center;
						padding-top: 36rpx;

						.left {
							display: flex;
							flex-direction: row;
							align-items: center;
							flex: 1;

							.avater {
								width: 40rpx;
								height: 40rpx;
								background: rgba(0, 0, 0, 0.00);
								border-radius: 50%;
								margin-right: 8rpx;
							}

							.leftName {
								font-size: 28rpx;
								font-family: PingFang SC, PingFang SC-Medium;
								font-weight: 500;
								text-align: left;
								color: #333333;
							}
						}

						.right {
							display: flex;
							flex-direction: row;
							align-items: center;

							.Class-record {
								transform: rotateZ(360deg);
								width: 152rpx;
								height: 64rpx;
								border: 1rpx solid #73F0EA;
								border-radius: 16rpx;
								font-size: 28rpx;
								font-family: PingFang SC, PingFang SC-Bold;
								font-weight: 700;
								color: #73F0EA;
								display: flex;
								align-items: center;
								justify-content: center;
								transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
								cursor: pointer;
								// 关闭点击高亮效果
								-webkit-tap-highlight-color: transparent;
								tap-highlight-color: transparent;
								outline: none;
								-webkit-touch-callout: none;
								-webkit-user-select: none;
								user-select: none;
														
								&:hover {
									transform: translateY(-2rpx) rotateZ(360deg);
									box-shadow: 0 8rpx 25rpx rgba(115, 240, 234, 0.3);
								}
														
								&:active {
									transform: scale(0.95) rotateZ(360deg);
									background-color: transparent !important;
									box-shadow: none !important;
								}
							}

							.Class-begins {
								transform: rotateZ(360deg);
								width: 152rpx;
								height: 64rpx;
								background: #73F0EA;
								border: 1rpx solid #73F0EA;
								border-radius: 16rpx;
								font-size: 28rpx;
								font-family: PingFang SC, PingFang SC-Bold;
								font-weight: 700;
								text-align: center;
								color: #ffffff;
								display: flex;
								align-items: center;
								justify-content: center;
								margin-left: 8rpx;
								transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
								cursor: pointer;
								// 关闭点击高亮效果
								-webkit-tap-highlight-color: transparent;
								tap-highlight-color: transparent;
								outline: none;
								-webkit-touch-callout: none;
								-webkit-user-select: none;
								user-select: none;
														
								&:hover {
									transform: translateY(-2rpx) rotateZ(360deg);
									box-shadow: 0 8rpx 25rpx rgba(115, 240, 234, 0.5);
									background: linear-gradient(135deg, #73F0EA 0%, #5ee8e1 100%);
								}
														
								&:active {
									transform: scale(0.95) rotateZ(360deg);
									background-color: #73F0EA !important;
									box-shadow: none !important;
								}
							}
						}
					}
				}
			}
		}
	}
	
	// 动画关键帧
	@keyframes fadeInUp {
		0% {
			opacity: 0;
			transform: translateY(30rpx);
		}
		100% {
			opacity: 1;
			transform: translateY(0);
		}
	}
	
	@keyframes pulse {
		0%, 100% {
			opacity: 0.4;
		}
		50% {
			opacity: 0.8;
		}
	}
	
	@keyframes skeleton-loading {
		0% {
			background-position: -200% 0;
		}
		100% {
			background-position: 200% 0;
		}
	}
	
	// 流光效果
	@keyframes shimmer {
		0% {
			background-position: -100% 0;
		}
		100% {
			background-position: 100% 0;
		}
	}
</style>